//
//  DatatoDataView.m
//  iDent
//
//  Created by Marius Corega on 6/5/09.
//  Copyright 2009 Marius Corega. All rights reserved.
//

#import "DatatoDataView.h"


@implementation DatatoDataView

+ (Class)transformedValueClass
{
    return [NSDate class];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)transformedValue:(id)value
{
    NSString *datainit = value;

   
	
    if (value == nil) return nil;

	/*
    // Attempt to get a reasonable value from the
    // value object.
    if ([value respondsToSelector: @selector(stringValue)]) {
		// handles NSString and NSNumber
        datainit = [value stringValue];
    } else {
        [NSException raise: NSInternalInconsistencyException
                    format: @"Value (%@) does not respond to -floatValue.",
		 [value class]];
    }
	
	 */
	NSArray *dataArray = [datainit componentsSeparatedByString:@"/"];
	NSString *year = [dataArray objectAtIndex:2];
	if([year length] <3) year = [NSString stringWithFormat:@"20%@", [dataArray objectAtIndex:2]];
	
	
	return [NSDate dateWithString:[NSString stringWithFormat:@"%@-%@-%@ 12:00:00 +0300", year, [dataArray objectAtIndex:1], [dataArray objectAtIndex:0]]];
	
	if([datainit length]>10) return [NSDate dateWithString:datainit];
    return [NSDate dateWithNaturalLanguageString:datainit];
}


- (id)reverseTransformedValue:(id)value
{
	NSDate *data = value;
	
    if (value == nil) return nil;
	
	/*
    // Attempt to get a reasonable value from the
    // value object.
    if ([value respondsToSelector: @selector(dateValue)]) {
		// handles NSString and NSNumber
        data = [value dateValue];
    } else {
        [NSException raise: NSInternalInconsistencyException
                    format: @"Value (%@) does not respond to -floatValue.",
		 [value class]];
    }
	 */
// NSString *str = [data descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S %z" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
	return [data descriptionWithCalendarFormat:@"%d/%m/%Y" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
}

@end
